package cn.zf233.xcloud.service.impl;

import cn.zf233.xcloud.commom.R;
import cn.zf233.xcloud.entity.System;
import cn.zf233.xcloud.mapper.SystemMapper;
import cn.zf233.xcloud.service.SystemService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by zf233 on 2021/8/5
 */
@Service
public class SystemServiceImpl implements SystemService {

    private static final String DEFAULT_BOOK_URL = "/system/XCloud.pdf";
    private static final String DEFAULT_APP_URL = "/system/xcloud_app.jpg";
    private static final String DEFAULT_STARTER_URL = "/system/xcloud_learn.png";
    private static final String DEFAULT_TOAST_INDEX = "<span><img src='https://www.xcloud.show/static/img/toast.png' width='15px' height='15px' style='vertical-align: middle'><span style='vertical-align: middle'>&nbsp;欢迎您体验XCloud</span></span>";
    private static final String DEFAULT_TOAST_HOME = SystemServiceImpl.DEFAULT_TOAST_INDEX;
    private System system = null;
    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    @Resource
    private SystemMapper systemMapper;

    @Override
    public R<String> getBookUrl() {
        String url;
        System system = getSystemEntity();
        if (system == null || StringUtils.isBlank(system.getBookUrl())) {
            url = DEFAULT_BOOK_URL;
        } else {
            url = system.getBookUrl();
        }
        return R.createBySuccess(url);
    }

    @Override
    public R<String> getAppUrl() {
        String url;
        System system = getSystemEntity();
        if (system == null || StringUtils.isBlank(system.getAppUrl())) {
            url = DEFAULT_APP_URL;
        } else {
            url = system.getAppUrl();
        }
        return R.createBySuccess(url);
    }

    @Override
    public R<String> getStarterUrl() {
        String url;
        System system = getSystemEntity();
        if (system == null || StringUtils.isBlank(system.getStarterUrl())) {
            url = DEFAULT_STARTER_URL;
        } else {
            url = system.getStarterUrl();
        }
        return R.createBySuccess(url);
    }

    @Override
    public R<System> getSystem() {
        System system = getSystemEntity();
        if (system == null) {
            return R.createByErrorMessage("系统配置参数获取失败");
        }
        return R.createBySuccess("系统配置参数获取成功", system);
    }

    @Override
    public R update(System system) {
        lock.writeLock().lock();
        try {
            this.system = null;
            system.setId(1L);
            if (!(system.getToastIndexEnable() == 0 || system.getToastIndexEnable() == 1)) {
                return R.createByErrorMessage("系统配置通知开关参数有误");
            }
            Integer result = systemMapper.updateByPrimaryKeySelective(system);
            if (result > 0) {
                return R.createBySuccessMessage("系统配置参数修改成功");
            }
            return R.createByErrorMessage("系统配置参数修改失败");
        } finally {
            lock.writeLock().unlock();
        }
    }

    @Override
    public R getIndexToast() {
        String toast;
        System system = getSystemEntity();
        if (system == null || StringUtils.isBlank(system.getToastIndex())) {
            toast = DEFAULT_TOAST_INDEX;
        } else {
            toast = system.getToastIndex();
        }
        Map<String, Object> map = new HashMap<>(2);
        map.put("toast", toast);
        map.put("enable", system == null ? 1 : system.getToastIndexEnable());
        return R.createBySuccess(map);
    }

    @Override
    public R getHomeToast() {
        String toast;
        System system = getSystemEntity();
        if (system == null || StringUtils.isBlank(system.getToastHome())) {
            toast = DEFAULT_TOAST_HOME;
        } else {
            toast = system.getToastHome();
        }
        Map<String, Object> map = new HashMap<>(2);
        map.put("toast", toast);
        map.put("enable", system == null ? 1 : system.getToastHomeEnable());
        return R.createBySuccess(map);
    }

    private System getSystemEntity() {
        lock.readLock().lock();
        try {
            if (system == null) {
                system = systemMapper.selectByPrimaryKey(1L);
            }
            return system;
        } finally {
            lock.readLock().unlock();
        }
    }
}
